﻿@using BusinessLibrary
@model IEnumerable<MMSWebsite.Models.Asset.Asset>

@{
    ViewBag.Title = "Detail & Asset";
    bool finishValuation = false, confirmLoan = false;
    double sumValuation = 0, sumLoan = 0;
    if(Model.Count() != 0)
    {
        finishValuation = true;
        confirmLoan = true;
        if (Model.First().Customer.FinishedDay != null)
        {
            finishValuation = false;
        }
    }
   
}
<table>
    <tr>
        <th>@Html.DisplayNameFor(model => model.AssetName)</th>
        <th>@Html.DisplayNameFor(model => model.Information)</th>
        <th>@Html.DisplayNameFor(model => model.AssetType)</th>
        <th>@Html.DisplayNameFor(model => model.Status)</th>
        <th>@Html.DisplayNameFor(model => model.Valuation)</th>
        <th>@Html.DisplayNameFor(model => model.Loan)</th>
        <th>@Html.DisplayNameFor(model => model.Rate)</th>
    </tr>
    @foreach (var item in Model)
    {
        <tr>
            <td>
                @if (Roles.Provider.IsUserInRole(User.Identity.Name, Constants.FV_ROLE) && item.Customer.FinishedDay != null)
                {
                   <span style="font-style: italic; color: darkcyan; font-weight: bold">@Html.DisplayFor(model => item.AssetName)</span>
                }
                else
                {
                     <a href="@Url.Action("Edit", "Asset", new { assetId = item.Id })" style="font-style: italic; color: darkcyan; font-weight: bold">
                        @Html.DisplayFor(model => item.AssetName)
                    </a>
                }
            </td>
            <td>@Html.DisplayFor(model => item.Information)</td>
            <td style="text-align: center;">@Html.DisplayFor(model => item.AssetType.name)</td>
            <td style="text-align: center; font-weight: bold">@Html.DisplayFor(model => item.Status.Last().Status.value)</td>
             
            @if (item.Valuation.Value == null)
            {//Check FV finish Valuation for all Asset
                finishValuation = false;
            }
            else
            {
                sumValuation += double.Parse(item.Valuation.Value.ToString());
            }
            <td>@Html.DisplayFor(model => item.Valuation.Value)</td>
            
            @if (item.Loan.ValueLoan == null)
            {//Check FV finish Loan&rate for all Asset
                confirmLoan = false;
            }
            else
            {
                sumLoan += double.Parse(item.Loan.ValueLoan.ToString());
            }
            <td>@Html.DisplayFor(model => item.Loan.ValueLoan)</td>
            <td>@Html.DisplayFor(model => item.Rate.ValueRate)</td>
        </tr>
    }
    <tr>
        <td colspan="4" style="text-align: right; font-weight: bold">Result</td>
        <td><span id="sumValualtion">@sumValuation</span></td>
        <td><span id="sumLoan">@sumLoan</span></td>
        <td></td>
    </tr>
</table>
<div style="text-align: center">
    @if (User.Identity.IsAuthenticated)
    {
        if (Roles.Provider.IsUserInRole(User.Identity.Name, Constants.FV_ROLE))
        {
            //if FV valuation all asset -> appear this button to finish
            if (finishValuation)
            {
                <input type="button" onclick="location.href = '@Url.Action("FVFinish", "Asset", new { cusId = Model.First().Customer.Id })'" value="Finish Valuation" />
            }
            //}
            //if (!Roles.Provider.IsUserInRole(User.Identity.Name, Constants.ADMIN_ROLE))
            //{
            if (confirmLoan)
            {
                <input type="button" onclick="location.href = '@Url.Action("RefuseLoan", "Asset", new { assetId = Model.First().Id })'" value="Refuse Loan" />
                <input type="button" onclick="location.href = '@Url.Action("ConfirmLoan", "Asset", new { assetId = Model.First().Id })'" value="Confirm Loan" />
        
            }
        }
        if (Roles.Provider.IsUserInRole(User.Identity.Name, Constants.REVIEWER_ROLE))
        {
            if (confirmLoan)
            {
                 <input type="button" onclick="location.href = '@Url.Action("SendEmailFinishLoan", "Asset", 
                 new { fvid = Model.First().Valuation.ValuationBy.Employee.Id, cusid = Model.First().Customer.Id})'" value="Send Email Fnish Loan To FV" />
             }
         }
    }
    
</div>
<script type="text/javascript">
    $(window).load(function () {
        $('#sumValualtion').text(formatCurrency($('#sumValualtion').text()));
        $('#sumLoan').text(formatCurrency($('#sumLoan').text()));
    });
    function formatCurrency(num) {
        num = num.toString().replace(/\$|\,/g, '');
        if (isNaN(num))
            num = "0";
        sign = (num == (num = Math.abs(num)));
        num = Math.floor(num * 100 + 0.50000000001);
        cents = num % 100;
        num = Math.floor(num / 100).toString();
        if (cents < 10)
            cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
            num = num.substring(0, num.length - (4 * i + 3)) + ',' +
                num.substring(num.length - (4 * i + 3));
        return (((sign) ? '' : '-') + '$' + num + '.' + cents);
    }
//  End -->
</script>
</script>
  